package com.chookss.home.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.video.VideoOnePlayActivity;
import com.chookss.video.entity.VideoEntity;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class SearchSmallVideoAdapter extends BaseQuickAdapter<SearchAllEntity.VideoBean.ListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String key;

    public SearchSmallVideoAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.VideoBean.ListBean> data) {
        super(layoutResId, data);
        this.context = context;

    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, SearchAllEntity.VideoBean.ListBean item) {
        TextView tvTiltle = helper.getView(R.id.tvSearchVideoTitle);
        TextView tvSearchVideoType = helper.getView(R.id.tvSearchVideoType);
        TextView tvSearchVideoProvider = helper.getView(R.id.tvSearchVideoProvider);
        TextView tvSearchVideoKeywords = helper.getView(R.id.tvSearchVideoKeywords);
        ImageView ivSearchVideo = helper.getView(R.id.ivSearchVideo);
        tvTiltle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getVideoTitle(), key));
        if (!Utils.isNull(item.getVideoCatalogName())) {
            tvSearchVideoType.setText("来源：");
            tvSearchVideoType.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getVideoCatalogName(), key));
        }
        if (!Utils.isNull(item.getProvideEmployeeName())) {
            tvSearchVideoProvider.setText("提供者：");
            tvSearchVideoProvider.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getProvideEmployeeName(), key));
        }
        if (!Utils.isNull(item.getVideoKeyWord())) {
            if (item.getVideoKeyWord().contains(key)) {
                tvSearchVideoKeywords.setVisibility(View.VISIBLE);
                tvSearchVideoKeywords.setText("关键词：");
                tvSearchVideoKeywords.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getVideoKeyWord(), key));

            } else {
                tvSearchVideoKeywords.setVisibility(View.GONE);
            }
        } else {
            tvSearchVideoKeywords.setVisibility(View.GONE);
        }
        GlideUtils.loadCustomRound(context, item.getVideoCoverPath(), ivSearchVideo, R.drawable.default_vedio, 4);
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getViedeo(item);
            }
        });
    }

    private void getViedeo(SearchAllEntity.VideoBean.ListBean item) {
        Map<String, String> map = new HashMap<>();
        if(!Utils.isNull(item.getVideoId())){
            map.put("videoId", item.getVideoId());
        }else{
            map.put("id", item.getId());
        }
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<VideoEntity>>() {
        }.getType(), null, Urls.selectAPPVideoById, SignUtils2.makeSignStr(map), false, new ObserverCallback<VideoEntity>() {
            @Override
            public void onSuccess(VideoEntity data) {
                if (Utils.isNull(data.videoId)) {
                    MyToast.show("视频id为空");
                    return;
                }
                Intent intent = new Intent(context, VideoOnePlayActivity.class);
                intent.putExtra("watermark", "2");
                intent.putExtra("video", data);
                context.startActivity(intent);
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
